package com.hzw.saas.web.app.config.mybatis;

import com.hzw.saas.common.config.configuration.BaseMetaObjectHandler;
import com.hzw.saas.common.security.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

/**
 * mybatis 自动填充配置
 * 增加了业务层代码
 *
 * @author zzl
 * @since 05/10/2021
 */
@Component
public class MyMetaObjectHandler extends BaseMetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        super.insertFill(metaObject);
        String userId = this.getUserId();
        this.strictInsertFill(metaObject, "creatorId", String.class, userId);
        this.strictInsertFill(metaObject, "editorId", String.class, userId);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        super.updateFill(metaObject);
        String userId = this.getUserId();
        this.strictUpdateFill(metaObject, "editorId", String.class, userId);
    }

    private String getUserId() {
        String userId;
        try {
            userId = SecurityUtils.getUser().getUserId();
        } catch (Exception e) {
            userId = DEFAULT_USER_ID;
        }
        return userId;
    }

}
